package com.nariano.server.utils.dbtest;

import java.util.logging.Logger;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.persistence.Query;


import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.support.JpaDaoSupport;

/**
 * Checks that Population mapping works
 */
public class PopulationCounter extends JpaDaoSupport {
	
	//private static final String DELETE_ORPHANS = "delete p from population p left join area_population as ap on p.population_id=ap.population_id left join city_population as cp on p.population_id=cp.population_id where cp.population_id is NULL and ap.population_id is NULL";
	//private static final String COUNT_ORPHANS = "select count(*) from population p left join area_population as ap on p.population_id=ap.population_id left join city_population as cp on p.population_id=cp.population_id where cp.population_id is NULL and ap.population_id is NULL";
	private static final String DELETE_ORPHANS = "delete p from population p left join area_population as ap on p.population_id=ap.population_id left join city_population as cp on p.population_id=cp.population_id where cp.population_id is NULL and ap.population_id is NULL";
	
	static Logger logger = Logger.getLogger(PopulationCounter.class.getCanonicalName());
	
	private PopulationCounter() {}
	
	public void check(String context) {
		//do nothing
		if (true) return;
		int orphansCount = deleteOrphans();
		if (orphansCount != 0) {
			logger.severe(String.format("%s : deleted %d orphans", context, orphansCount));
		}
		
	}
	
	private int deleteOrphans() {
		Object res = getJpaTemplate().execute(new JpaCallback() {
			
			@Override
			public Object doInJpa(EntityManager em) throws PersistenceException {
				Query q = em.createNativeQuery(DELETE_ORPHANS);
				return q.executeUpdate();
			}
		});
		/*Object res = ht.execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				SQLQuery q = session.createSQLQuery(DELETE_ORPHANS);
				return q.executeUpdate();
			}
		});*/
		return res==null?0:((Integer)res).intValue();
	}

	/*private static int countRows(HibernateTemplate ht) {
		Object res = ht.execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				SQLQuery q = session.createSQLQuery(COUNT_ORPHANS);
				return q.uniqueResult();
			}
		});
		return res==null?0:((BigInteger)res).intValue();
	}*/

	/*private static int countRows(HibernateTemplate ht, final String table) {
		Object res = ht.execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				SQLQuery q = session.createSQLQuery("select count(*) from "+table);
				return q.uniqueResult();
			}
		});
		return res==null?0:((BigInteger)res).intValue();
	}
	*/
	

}
